package com.mxpio.erp.inventory.controller;

import com.mxpio.erp.common.inventory.entity.InventoryReview;
import com.mxpio.erp.common.inventory.service.InventoryReviewService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import com.mxpioframework.jpa.query.Order;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "InventoryScopeController", description = "盘点单盘点复核记录接口")
@RestController
@RequestMapping("/erp/inventory/inventoryreview")
public class InventoryReviewController {

    @Autowired
    private InventoryReviewService inventoryReviewService;

    @GetMapping("list")
    @Operation(summary = "盘点复核记录列表", description = "获取盘点复核列表（无分页）", method = "GET")
    public Result<List<InventoryReview>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<InventoryReview> items = inventoryReviewService.list(InventoryReview.class, criteria);
        return Result.OK(items);
    }

    @GetMapping("page")
    @Operation(summary = "盘点复核记录列表", description = "获取盘点复核记录列表（分页）", method = "GET")
    public Result<Page<InventoryReview>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<InventoryReview> itemPage = inventoryReviewService.listPage(InventoryReview.class, page, criteria);
        return Result.OK(itemPage);
    }
    @GetMapping("list/{inventoryCode}")
    @Operation(summary = "根据inventoryCode获取盘点复核记录", description = "根据inventoryCode获取盘点复核记录", method = "GET")
    public Result<List<InventoryReview>> getById(@Parameter(description="业务单号") @PathVariable(name = "inventoryCode", required = true) String inventoryCode) {
        Criteria c = Criteria.create().addCriterion("inventoryCode", Operator.EQ, inventoryCode);
        c.addOrder(new Order("createTime",true));//按创建时间降序排列
        List<InventoryReview> items = inventoryReviewService.list(InventoryReview.class, c);
        return Result.OK(items);
    }


    @PostMapping("add")
    @Operation(summary = "新增盘点复核记录", description = "新增盘点复核记录", method = "POST")
    public Result<InventoryReview> add(@RequestBody InventoryReview item) {
        inventoryReviewService.save(item);
        return Result.OK(item);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑盘点复核记录", description = "编辑盘点复核记录", method = "PUT")
    public Result<InventoryReview> edit(@RequestBody InventoryReview warehouseOrderLine) {
        inventoryReviewService.update(warehouseOrderLine);
        return Result.OK(warehouseOrderLine);
    }

    @DeleteMapping("remove/{ids}")
    @Operation(summary = "根据id删除盘点复核记录", description = "删除盘点复核记录,多个id用英文逗号分隔,bizNo和lineNo间使用英文冒号分隔,bizNo在前,lineNo在后", method = "DELETE")
    public Result<InventoryReview> remove(@PathVariable(name = "ids", required = true) String ids) {
        String[] id = ids.split(",");
        for(String key : id){
            inventoryReviewService.remove(InventoryReview.class, key);
        }
        return Result.OK();
    }

}
